package net.fdukedom.epicurus.client.ui.dialogs;

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import net.fdukedom.epicurus.client.util.ConfigurationHelper;
/**
 * 
 * @author Alexander Kirakozov
 */
public class ConnectOptionDlg extends ModalDialog {

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

    /**
     * Server url text field
     */
    private JTextField serverURLField;

    /**
     * Action listener for saving connection options 
     * @author Alexander Kirakozov
     */
    private class SaveActionListener implements ActionListener {
        public void actionPerformed(ActionEvent arg0) {
            ConfigurationHelper config = new ConfigurationHelper();
            
            config.setServerURL(serverURLField.getText());
            config.save();
            close();
        }
    }

    /**
     * Constructor 
     * @param frame main frame
     */
    public ConnectOptionDlg(JFrame frame) {
        super(frame);
        setContentPane(createPanel());
        init("Connection option", frame);
    }

    /**
     * Creates panel of dialog
     * @return created panel
     */
    protected JPanel createPanel() {
        ConfigurationHelper config = new ConfigurationHelper();
        
        // Configuration text fields & labels
        JPanel box = new JPanel();
        box.setLayout(new GridLayout(1, 2));

        box.add(new JLabel("Server URL"));
        serverURLField = new JTextField(config.getServerURL());
        box.add(serverURLField);

        // Ok & cancel buttons
        JPanel buttons = new JPanel();
        buttons.setLayout(new FlowLayout());

        JButton button = new JButton("Ok");
        button.addActionListener(new SaveActionListener());
        buttons.add(button);

        button = new JButton("Cancel");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                close();
            }
        });
        buttons.add(button);

        // Main panel
        JPanel panel = new JPanel();

        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        panel.add(box);
        panel.add(buttons);

        return panel;
    }
}
